package hn.sigit.view;

import hn.sigit.model.hnd.administrative.HND_User;
import hn.sigit.model.hnd.administrative.dao.HND_UserDAO;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("usersHelper")
@Scope(value = ScopeType.CONVERSATION)
@AutoCreate
public class UsersHelper {
	private List<HND_User> users;
	private String password;
	private String confPassword;
	private HND_User selected;
	private int currentRow;

	public List<HND_User> getUsers() {
		if (users == null)
			users = HND_UserDAO.loadUsers(true);
		
		return users;
	}
	public void setUsers(List<HND_User> users) {
		this.users = users;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getConfPassword() {
		return confPassword;
	}
	public void setConfPassword(String confPassword) {
		this.confPassword = confPassword;
	}
	public HND_User getSelected() {
		return selected;
	}
	public void setSelected(HND_User selected) {
		this.selected = selected;
	}
	
	public int getCurrentRow() {
		return currentRow;
	}
	public void setCurrentRow(int currentRow) {
		this.currentRow = currentRow;
	}
	
	
	public String deleteSelectedUser() {
		HND_User user = getUsers().get(currentRow);
		user.setActive(false);
		HND_UserDAO.save(user);
		users.remove(currentRow);
		
		return null;
	}
	
	public String applyChange() {
		if (selected == null) return null;
		
		if (password.equals(confPassword)) {
			selected.setPassword(password);
			HND_UserDAO.save(selected);
		}
		else {
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Las contrasenas no coinciden", "Asegurese que coincidan");
			FacesContext.getCurrentInstance().addMessage(null, message);
		}
		
		return null;
	}
}
